#include "squareloopdrawable.h"

SquareLoopDrawable::SquareLoopDrawable()
{
}

SquareLoopDrawable::SquareLoopDrawable(QPointF center, double side)
{
    _center = center;
    _side = side;
    resetColor();
}

void SquareLoopDrawable::draw(CoodrsTransformer *coordsTransformer)
{
    glLineWidth(LINE_WIDTH);
    glBegin(GL_LINE_STRIP);
    glColor3d(_color.red(), _color.green(), _color.blue());
    QPointF coords = _center;
    if(coordsTransformer)
    {
        coords = coordsTransformer->transform(coords);
    }
    glVertex2d((coords.x() - _side), (coords.y() + _side));
    glVertex2d((coords.x() + _side), (coords.y() + _side));
    glVertex2d((coords.x() + _side), (coords.y() - _side));
    glVertex2d((coords.x() - _side), (coords.y() - _side));
    glVertex2d((coords.x() - _side), (coords.y() + _side));
    glEnd();
    glLineWidth(1);
}

QRectF SquareLoopDrawable::getBoundingBox()
{
    QPointF topLeft = QPointF((_center.x() - _side), (_center.y() - _side));
    QPointF rightBottom = QPointF((_center.x() + _side), (_center.y() + _side));
    return QRectF(topLeft, rightBottom);
}

bool SquareLoopDrawable::isSelected(QPointF selectedPoint)
{
    QRectF boundary = getBoundingBox();
    if(boundary.contains(selectedPoint))
    {
        return true;
    }
    return false;
}
